package com.nfc.lock.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.nfc.lock.entity.OperationLog;
import com.nfc.lock.mapper.OperationLogMapper;
import com.nfc.lock.service.IOperationLogService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

/**
 * 操作日志Service实现
 */
@Service
@RequiredArgsConstructor
public class OperationLogServiceImpl extends ServiceImpl<OperationLogMapper, OperationLog>
        implements IOperationLogService {

    private final OperationLogMapper operationLogMapper;

    @Override
    public IPage<OperationLog> getLogList(Integer pageNum, Integer pageSize, Integer operationType,
                                          Long userId, String lockNfcId,
                                          LocalDateTime startTime, LocalDateTime endTime) {
        LambdaQueryWrapper<OperationLog> wrapper = new LambdaQueryWrapper<>();
        if (operationType != null) {
            wrapper.eq(OperationLog::getOperationType, operationType);
        }
        if (userId != null) {
            wrapper.eq(OperationLog::getUserId, userId);
        }
        if (lockNfcId != null && !lockNfcId.isEmpty()) {
            wrapper.like(OperationLog::getLockNfcId, lockNfcId);
        }
        if (startTime != null) {
            wrapper.ge(OperationLog::getOperationTime, startTime);
        }
        if (endTime != null) {
            wrapper.le(OperationLog::getOperationTime, endTime);
        }
        wrapper.orderByDesc(OperationLog::getOperationTime);

        Page<OperationLog> page = new Page<>(pageNum, pageSize);
        return operationLogMapper.selectPage(page, wrapper);
    }
}
